package utils;

import org.junit.Test;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * ClassName:JDBCutils
 * Package:JDBCutils
 * Description
 *
 * @Author:@wenxueshi
 * @Create:2022/11/30 - 16:09
 * @Version:v1.0
 */
public class JDBCutils {
    @Test
    public void test()
    {
        String sql = "select * from eqipments";
        List<eqipments> select = getSelect(eqipments.class, sql);
        select.forEach(System.out :: println);
    }
    private static DataSource source;
    static
    {
        try {
            FileInputStream is = new FileInputStream(new File("src//druid.properties"));
            Properties pros = new Properties();
            pros.load(is);
            source = DruidDataSourceFactory.createDataSource(pros);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取数据库连接
     * @return
     * @throws Exception
     */
    public static Connection getConnection() throws Exception {
        Connection conn = source.getConnection();
        return conn;
    }

    /**
     * 查询不同表的操作
     * @param clazz
     * @param sql
     * @param args
     * @return
     * @param <T>
     */
    public static <T> List<T> getSelect(Class<T> clazz,String sql,Object ...args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            //预编译sql语句
            ps = conn.prepareStatement(sql);
            //填充占位符
            for(int i=0;i<args.length;i++)
            {
                ps.setObject(i+1,args[i]);
            }
            //获取结果集
            rs = ps.executeQuery();
            //获取元数据
            ResultSetMetaData remd = rs.getMetaData();
            //获取结果集中的列数
            int columnCount = remd.getColumnCount();
            ArrayList<T> list = new ArrayList<>();
            while(rs.next())
            {
                //获取当前运行时类的对象
                T t = clazz.newInstance();
                for(int i=0;i<columnCount;i++)
                {
                    //获取列值
                    Object columnvalue = rs.getObject(i + 1);
                    //获取列名
                    String columnLabel = remd.getColumnLabel(i + 1);
                    //通过反射获取属性
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t,columnvalue);
                }
                list.add(t);
            }

            return list;

        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            DbUtils.closeQuietly(conn);
            DbUtils.closeQuietly(ps);
            DbUtils.closeQuietly(rs);
        }
        return null;
    }
}
